package com.jy.datapipeline.export.dao.impl;

import com.jy.datapipeline.export.dao.DataStoreInfoDao;
import com.jy.datapipeline.export.entity.DataStoreInfoModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

@Slf4j
@Repository
public class DataStoreInfoDaoImpl implements DataStoreInfoDao {
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Override
    public List<Map<String, Object>> findDataStoreInfoByList(String storeName, String storeType,
        int iDisplayStart, int iDisplayLength) {

        List<Map<String, Object>> list=new ArrayList<>();
        try {
            StringBuffer sb=new StringBuffer();
            Map<String,Object> params = new HashMap<String,Object>();
            sb.append("select a.id,a.host,a.password,a.remote_path,a.store_id,a.store_name,a.store_type, DATE_FORMAT(a.update_time,'%Y-%m-%d %H:%i:%s') as update_time ,a.username from data_store_info a where 1=1 ");
            if(storeName!=null && !storeName.equals("")){
                sb.append(" and a.store_name like :storeName");
                params.put("storeName","%"+storeName+"%");
            }
            if(storeType!=null && !storeType.equals("")){
                sb.append(" and a.store_type =:storeType");
                params.put("storeType",storeType);
            }
            sb.append(" order by a.update_time desc  ");
            if(iDisplayStart>-1 && iDisplayLength>-1){
                sb.append("LIMIT  :iDisplayStart ,:iDisplayLength ;");
                params.put("iDisplayStart",iDisplayStart);
                params.put("iDisplayLength",iDisplayLength);
            }

            list=this.namedParameterJdbcTemplate.queryForList(sb.toString(),params);
        }catch (Exception e){
            log.error("findDataStoreInfoByList exception",e);
        }

        return list;
    }

    @Override
    public int findDataStoreInfoByCount(String storeName, String storeType) {
        int count=0;
        List<Map<String, Object>> list=new ArrayList<>();
        try {
            StringBuffer sb=new StringBuffer();
            Map<String,Object> params = new HashMap<String,Object>();
            sb.append("select COUNT(a.id) as count from data_store_info a where 1=1 ");
            if(storeName!=null && !storeName.equals("")){
                sb.append(" and a.store_name like :storeName");
                params.put("storeName","%"+storeName+"%");
            }
            if(storeType!=null && !storeType.equals("")){
                sb.append(" and a.store_type =:storeType");
                params.put("storeType",storeType);
            }
            list=this.namedParameterJdbcTemplate.queryForList(sb.toString(),params);
            if(list.size()>0){
                count=Integer.parseInt(list.get(0).get("count")!=null?list.get(0).get("count").toString():"0");
            }
        }catch (Exception e){
            log.error("findDataStoreInfoByList exception",e);
        }

        return count;
    }

    @Override
    public boolean updateDataStoreInfo(DataStoreInfoModel dsim) {
        Map<String,Object> params = new HashMap<String,Object>();
        StringBuilder sb=new StringBuilder();
        sb.append("update data_store_info set store_name=:store_name,store_type=:store_type,"
            + " username=:username,password=:password,host=:host,remote_path=:remote_path,update_time=NOW()"
            + " where store_id=:store_id");
        params.put("store_name",dsim.getStoreName());
        params.put("store_type",dsim.getStoreType());
        params.put("username",dsim.getUserName());
        params.put("password",dsim.getPassword());
        params.put("host",dsim.getHost());
        params.put("remote_path",dsim.getRemotePath());
        params.put("store_id",dsim.getStoreId());
        int req= this.namedParameterJdbcTemplate.update(sb.toString(), params);
        return req>0?true:false;
    }
}
